package com.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.domain.bo.ResearchServiceBo;
import com.domain.entity.ResearchService;
import com.domain.vo.FileInfos;
import com.domain.vo.NavigationVo;
import com.domain.vo.ResServListVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ResearchServMapper extends BaseMapper<ResearchService> {

    /**
     * 统计
     * @param queryWrapper
     * @param researchServiceBo
     * @return
     */
    @Select({
            "<script>",
            "SELECT sdd.dict_label AS className, sdd.dict_value as classValue, COALESCE(COUNT(zbAll.CHECKSTATUS), 0) AS classCount ",
            "FROM ",
            "(SELECT DISTINCT dict_label,dict_value FROM sys_dict_data where dict_type ='bpm_process_instance_status') AS sdd  ",
            "LEFT join ",
            "(select zb.* from research_service zb ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='researchServiceBo.serviceType != null and researchServiceBo.serviceType.trim() != \"\"'>",
            "and zb.SERVICE_TYPE in ('${researchServiceBo.serviceType}') ",
            "</if>",
            " ) zbAll ON zbAll.CHECKSTATUS = sdd.dict_value  ",
            "GROUP BY sdd.dict_label, sdd.dict_value ORDER BY sdd.dict_value",
            "</script>"
    })
    List<NavigationVo.NameAndCount> selectGroups(@Param(Constants.WRAPPER) Wrapper<NavigationVo.NameAndCount> queryWrapper, @Param("researchServiceBo") ResearchServiceBo researchServiceBo);

    /**
     * 列表 + 导出
     * @param page
     * @param queryWrapper
     * @param researchServiceBo
     * @return
     */
    @Select({
            "<script>",
            "select zb.*, Date(APPLY_TIME) as applyTime,bu.NAME as unitName,dc.dict_label as checkstatusName,ps1.NAME as serviceTypeName from research_service zb ",
            "left join biz_unit bu on zb.UNIT_ID = bu.id ",
            "left join (select dict_label,dict_value from sys_dict_data WHERE dict_type = 'bpm_process_instance_status') dc on dc.dict_value = zb.CHECKSTATUS ",
            "left join (select * from CFG_CATEGORY_ENTRY where CATEGORYID = (select ID from cfg_category where BIZNAME ='科研服务类型')) ps1 on ps1.code = zb.SERVICE_TYPE ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='researchServiceBo.serviceType != null and researchServiceBo.serviceType.trim() != \"\"'>",
            "and zb.SERVICE_TYPE in ('${researchServiceBo.serviceType}') ",
            "</if>",
            "order by zb.CREATEDATE desc",
            "</script>"
    })
    IPage<ResServListVo> selectListInfos(IPage<ResServListVo> page, @Param(Constants.WRAPPER) Wrapper<ResServListVo> queryWrapper, @Param("researchServiceBo") ResearchServiceBo researchServiceBo);

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Select({
            "<script>",
            "select zb.*,Date(APPLY_TIME) as applyTime, suf.UPLOADFILENAME as filename from research_service zb ",
            "left join sys_upload_file suf on zb.FILE_ID = suf.ID ",
            "where zb.ID = '${id}'",
            "</script>"
    })
    ResearchService selectResearchServiceByID(@Param("id") String id);

    @Select({
            "select ID as fileId,UPLOADFILENAME as filename from sys_upload_file where ID in ('${fileIds}')"
    })
    List<FileInfos> getFileInfos(@Param("fileIds") String fileIds);
}
